package com.maigao.api.controller;

import com.maigao.api.interceptors.identify.Identify;
import com.maigao.entity.ResultJson;
import com.maigao.entity.form.AddUserForm;
import com.maigao.entity.jpa.User;
import com.maigao.entity.vo.ClassRecordVO;
import com.maigao.entity.vo.CoachVO;
import com.maigao.entity.vo.StudentVO;
import com.maigao.entity.vo.UserVO;
import com.maigao.service.ClassRecordService;
import com.maigao.service.UserService;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2024-10-16 23:34
 **/
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassRecordService classRecordService;

    @Identify
    @ResponseBody
    @RequestMapping("add")
    public ResultJson<String> addUser(AddUserForm addUserForm) {
        Triple<Boolean, String, User> addResult = userService.tryAddUser(addUserForm);
        if (addResult.getLeft()) {
            return ResultJson.success(addResult.getRight());
        }
        return ResultJson.fail(addResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("find")
    public ResultJson<UserVO> findUser(@RequestParam String mobile) {
        UserVO user = userService.findUserDetailByMobile(mobile);
        if (null == user) {
            return ResultJson.fail("未查询到数据");
        }
        return ResultJson.success(user);
    }

    @Identify
    @ResponseBody
    @RequestMapping("findUserDetail")
    public ResultJson<UserVO> findUserDetail(@RequestParam String uid) {
        Triple<Boolean, String, UserVO> findResult = userService.findUserDetail(uid);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findAllStudent")
    public ResultJson<List<StudentVO>> findAllStudent(@RequestParam String uid) {
        Triple<Boolean, List<StudentVO>, String> findResult = userService.findAllStudentByUid(uid);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getMiddle());
        }
        return ResultJson.fail(findResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("findCoach")
    public ResultJson<CoachVO> findCoach(@RequestParam String uid) {
        Triple<Boolean, CoachVO, String> findResult = userService.findCoachByUid(uid);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getMiddle());
        }
        return ResultJson.fail(findResult.getRight());
    }

    /**
     * 学生端使用, 根据传入日期区间查询该账号下学生上课列表
     *
     * @return
     */
    @Identify
    @ResponseBody
    @RequestMapping("findStudentClassRecord")
    public ResultJson findStudentClassRecords(@RequestParam String fromTime, @RequestParam String endTime) {
        Triple<Boolean, String, List<ClassRecordVO>> findResult = classRecordService.findStudentClassRecords(fromTime, endTime);
        if (findResult.getLeft()) {
            return ResultJson.success(findResult.getRight());
        }
        return ResultJson.fail(findResult.getMiddle());
    }

    @Identify
    @ResponseBody
    @RequestMapping("upgrade")
    public ResultJson upgrade2Admin(@RequestParam Integer coachId) {
        Pair<Boolean, String> upgradeResult = userService.upgrade2Admin(coachId);
        if (upgradeResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(upgradeResult.getRight());
    }

    @Identify
    @ResponseBody
    @RequestMapping("reduce")
    public ResultJson reduce2Normal(@RequestParam String uid) {
        Pair<Boolean, String> reduceResult = userService.reduce2Normal(uid);
        if (reduceResult.getLeft()) {
            return ResultJson.success("");
        }
        return ResultJson.fail(reduceResult.getRight());
    }

}
